package com.jiusit.task;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.jiusit.common.DataCoding;
import com.jiusit.common.HttpClientUtil;
import com.jiusit.common.JPushUtil;
import com.webbuilder.common.Var;
import com.webbuilder.utils.DateUtil;
import com.webbuilder.utils.DbUtil;
import com.webbuilder.utils.SysUtil;



public class ComplateOrdersTask implements Job{

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		System.out.println("平仓任务开始" + new Date());
		
		//String HOST = "http://www.tianqisoft.com/wjy_admin/api";
		
		Connection conn = null;		
		
		ResultSet orderRs = null;
		ResultSet urlRs = null;
		try{
			conn = DbUtil.getConnection();
			conn.setAutoCommit(false);
			
			String urlSql = "SELECT * FROM `wb_var` WHERE var_name = 'api_url'";
			urlRs = DbUtil.getResultSet(conn, urlSql);
			
			if(urlRs.next()){
				String orderSql = "select a.* from wjy_order a where a.order_status = '01' and a.ping_time <= now()";
				orderRs = DbUtil.getResultSet(conn, orderSql);
			    while(orderRs.next()){
			    	Map<String, Object> send = new HashMap();
					send.put("command", "orderResult");
					Map<String, Object> data = new HashMap();
					data.put("order_id", orderRs.getString("id"));
					data.put("is_win", "03");
					send.put("data", data);
					JSONObject json = new JSONObject(send);
					System.out.println("json: "+json.toString());
					try {
						//des加密在发送
						String sendString = DataCoding.encode(json.toString(), "XcKi0k89");
						System.out.println("json: "+sendString);
						Map<String, Object> argsMap = new HashMap();
						argsMap.put("data", sendString);
						String ret = HttpClientUtil.POSTMethod(urlRs.getString("var_value")+"/cmd", argsMap, null);
						System.out.println("ret raw: "+ret);
						String retas = DataCoding.decode(ret);
						System.out.println("ret decode: "+retas+"\n");					
					} catch (Exception e) {
						e.printStackTrace();
					}
			    }
			}

		}catch(Exception e){
			DbUtil.closeConnection(conn,true);
			e.printStackTrace();
		}finally{
			DbUtil.closeResultSet(orderRs);
			DbUtil.closeResultSet(urlRs);
			DbUtil.closeConnection(conn, false);
		}
		System.out.println("平仓任务开始,结束" + new Date());
	}
	
}
